<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>px转换成vw</title>
    <style>
      #app {
        width: 600px;
        margin: 0 auto;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <div>
        <input type="text" v-model="inpVal" />
      </div>
      <p>{{ calcRes }}</p>
      <button @click="copyFn">复制</button>
    </div>

    <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.12/vue.min.js"></script>
    <script>
      const app = new Vue({
        el: '#app',
        data: {
          inpVal: '',
          baseWidth: 1920
        },
        computed: {
          calcRes() {
            return ((this.inpVal / this.baseWidth) * 100).toFixed(5) + 'vw';
          }
        },
        methods: {
          copyFn() {
            const _input = document.createElement('input'); // 直接构建input
            _input.value = this.calcRes; // 设置内容
            document.body.appendChild(_input); // 添加临时实例
            _input.select(); // 选择实例内容
            document.execCommand('Copy'); // 执行复制
            document.body.removeChild(_input); // 删除临时实例
          }
        }
      });
    </script>
  </body>
</html>
